var error = {
  TIMEOUT: 3,
  POSITION_UNAVAILABLE: 2,
  PERMISSION_DENIED: 1,
  UNKNOWN_ERROR: 0
}
import geohash from 'ngeohash'
const home = {
  data() {
      return {
          guessCity: '',
          locationName: '',
          loaded: true,
          geohash: '',
          hotcity: [], //热门城市列表
          groupcity: {}, //所有城市列表
      };
  },
  created() {
      this.initData();
  },
  computed: {
      //将获取的数据按照A-Z字母开头排序
      sortgroupcity() {
          let sortobj = {};
          for (let i = 65; i <= 90; i++) {
              if (this.groupcity[String.fromCharCode(i)]) {
                  sortobj[String.fromCharCode(i)] = this.groupcity[String.fromCharCode(i)];
              }
          }
          return sortobj
      }
  },
  mounted() {
      this.get()
  },
  methods: {
      async initData() {
          let vm = this;
          await this.getLocation(function(position) {
              vm.geohash = geohash.encode(position.lat, position.lon, 12)
              // 获取当前位置信息
              vm.$fetch('GET', '/v1/pois', {
                  // wx4snhtu7pfu
                  // ww8p1r4t8yd0
                  'geohash': vm.geohash,
                  type: 'geohash'
              }).then(res => {
                  vm.locationName = res[0].name;
              })
          }, function(error) {
              switch (error.code) {
                  case error.TIMEOUT:
                      console.warn("A timeout occured! Please try again!");
                      break;
                  case error.POSITION_UNAVAILABLE:
                      console.warn('We can\'t detect your location. Sorry!');
                      break;
                  case error.PERMISSION_DENIED:
                      console.warn('Please allow geolocation access for this to work.');
                      break;
                  case error.UNKNOWN_ERROR:
                      console.warn('An unknown error occured!');
                      break;
              }
          });

      },
      /**获取当前位置经纬度有偏差，200m~300m*/
      getLocation(successFunc, errorFunc) {
          if (navigator.geolocation) {
              let pos_opt = {
                  // 指示浏览器获取高精度的位置，默认为false  
                  enableHighAcuracy: true,
                  // 指定获取地理位置的超时时间，默认不限时，单位为毫秒  
                  timeout: 5000,
                  // 最长有效期，在重复获取地理位置时，此参数指定多久再次获取位置。  
                  // maximumAge: 3000
              }
              navigator.geolocation.getCurrentPosition(locationSuccess, locationError, pos_opt);

              /**定位成功 */
              function locationSuccess(position) {
                  let data = {};
                  data['lat'] = position.coords.latitude;
                  data['lon'] = position.coords.longitude;
                  if (typeof successFunc == "function") {
                      successFunc(data);
                  }
              }

              /**定位失败 */
              function locationError(error) {
                  if (typeof error == "function") {
                      errorFunc(error);
                  }
              }
          } else {
              alert("Your browser does not support Geolocation!");
          }
      },
      get() {
          let vm = this;
          // token
          // this.$fetch('GET', 'v1/csrf_token', {}).then(res => {
          //     // console.log(res)
          // }).then(() => {

          // });
          // // 
          // this.$fetch('GET', 'v1/user?extras%5B%5D=premium_vip', {}).then(res => {
          //     // console.log(res)
          // }).then(() => {

          // });

          // 默认城市所在地
          this.$fetch('GET', 'v1/cities', {
              'type': 'guess'
          }).then(res => {
              this.guessCity = res;
          });

          // 热门城市
          this.$fetch('GET', 'v1/cities', {
              'type': 'hot'
          }).then(res => {
              this.hotcity = res;
          })

          // 城市列表
          this.$fetch('GET', 'v1/cities', {
              'type': 'group'
          }).then(res => {
              this.groupcity = res;
          });
      },
      toSist() {
          this.$router.push({
              path: '/msite',
              query: {
                  geohash: this.geohash
              }
          });
      }
  }
}

export default home;